package org.frog.sgs.client.process.all;

import org.apache.mina.core.session.IoSession;
import org.frog.sgs.card.basic.Basic;
import org.frog.sgs.card.equipment.Equipment;
import org.frog.sgs.card.scheme.type.JudgeType;
import org.frog.sgs.card.strength.StrengthFactory;
import org.frog.sgs.character.Character;
import org.frog.sgs.client.game.ClientInstance;
import org.frog.sgs.client.process.ClientProcessor;
import org.frog.sgs.data.GameDataManager;
import org.frog.sgs.io.SgsMsg;

/**
 * @author duanxiangqi
 * @date 2012-10-26
 */
public class ClientProcessorSyncAllCharacterInfo implements ClientProcessor {

	public void process(IoSession session, SgsMsg msg, ClientInstance instance) throws Exception {
		instance.getCharacters().clear();
		int characterSize = msg.readInt();
		for (int i = 0; i < characterSize; i++) {
			Character character = new Character();
			character.setPosition(msg.readInt());
			boolean isZhugong = msg.readBoolean();
			character.setDie(msg.readBoolean());
			if (isZhugong || character.isDie()) {
				character.setRole(GameDataManager.instance.getRoleCopy(msg.readInt()));
			}
			character.setGeneral(GameDataManager.instance.getGeneralCopy(msg.readInt()));
			int maxStrength = character.getGeneral().getMaxStrength();
			if (isZhugong) {
				maxStrength++;
			}
			character.setStrength(StrengthFactory.instance.newInstance(maxStrength));
			character.getStrength().setStrength(msg.readInt());
			int cardsInHandSize = msg.readInt();
			for (int j = 0; j < cardsInHandSize; j++) {
				character.getCardsInHand().put(j, null);
			}
			int equipmentSize = msg.readInt();
			for (int j = 0; j < equipmentSize; j++) {
				int position = msg.readInt();
				Equipment equipment = (Equipment) GameDataManager.instance.getBasicCopy(msg.readInt());
				character.getEquipments().put(position, equipment);
			}
			int judgeSchemeSize = msg.readInt();
			for (int j = 0; j < judgeSchemeSize; j++) {
				int judgeId = msg.readInt();
				int cardId = msg.readInt();
				Basic card = GameDataManager.instance.getBasicCopy(cardId);
				JudgeType judgeType = JudgeType.get(judgeId);
				character.getJudgeSchemes().put(judgeType, card);
			}
			instance.getCharacters().put(character.getPosition(), character);
		}
		instance.showAll();
	}
}
